﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Lost{
	/// <summary>
	/// 物品层级，ItemCategoryHierarchy的缩写
	/// -1层--什么都没有
	/// 0层--……
	/// </summary>
	public class ICH {
		///从-1开始算的层数
		public int HierarchyIndex = -1;
		///当IsLeaf==true时，为物品名；否则为类名
		public string Name = "";
		///ItemTag脚本
		public List<ItemTag> ItemTags = new List<ItemTag>();
		///子物品名
		public List<ICH> Children = new List<ICH>();
		///此节点下的ItemTag数目
		public int CountAll{get{return countAll;}}
		int countAll = 0;
		///父节点
		public ICH Parent;
		public ICH(){
		}
		public ICH(int hie, string newName){
			HierarchyIndex = hie;
			Name = newName;
		}
		///递归插入
		public void Insert(ItemTag item){
			if (HierarchyIndex != -1) {
				Debug.LogError ("You must insert a ItemTag from root node!");
			}
			string[] names = item.ItemName.Split (GlobalValue.NameDivSymbol);
			SetHierarchy (names, 0, item);
		}
		void SetHierarchy(string[] names, int childHie, ItemTag item){
			if (childHie < names.Length) {
				int childIndex = GetChildIndex (names [childHie]);
				if (childIndex == -1) {
					ICH newICH = new ICH (childHie, names [childHie]);
					childIndex = AddChild (newICH);
				}
				Children [childIndex].SetHierarchy (names, childHie + 1, item);
				countAll++;
			} else {
				//if (ItemTags != null && ItemTags.Count > 0) {
					//表明有同名物品
				//}
				//添加ItemTag
				ItemTags.Add(item); 
			}
		}
		///当不是叶子节点时是否为空
		bool IsEmpty{
			get{ return ((ItemTags==null || ItemTags.Count==0) && (Children == null || Children.Count == 0));}
		}
		///当不是叶子节点且为空时删除该节点
		bool RemoveWhileEmpty(){
			if (IsEmpty) {
				RemoveSelf ();
				return true;
			}
			return false;
		}
		///删除自己
		private void RemoveSelf(){
			Children.Clear ();
			Parent.RemoveChildICH (this);
		}
		///删除子物品名
		void RemoveChildICH(ICH childICH){
			Children.Remove (childICH);
			RemoveWhileEmpty ();
		}
		///删除ItemTag
		void RemoveItemTags(){
			ItemTags.Clear ();
			RemoveWhileEmpty ();
		}
		///子节点中是否包含这个名字
		///包含则返回对应的索引，否则返回-1
		public int GetChildIndex(string childName){
			int i = 0;
			for (; i < Children.Count; i++) {
				if (childName == Children [i].Name) {
					return i;
				}
			}
			return -1;
		}
		///添加孩子
		int AddChild(ICH newChild){
			Children.Add (newChild);
			int childIndex = Children.Count - 1;
			Children [childIndex].Parent = this;
			return childIndex;
		}
		///递归返回此节点下所有的ItemTag
		public ItemTag[] GetAllItemTags(){
			ItemTag[] tags = new ItemTag[CountAll];
			GetAllItemTags (tags, 0);
			return tags;
		}
		void GetAllItemTags(ItemTag[] tags, int curIndex){
			//当CountAll <= 0时，表明这是叶子节点
			if (CountAll <= 0) {
				return;
			}
			int childNum = Children.Count;
			for (int i = 0; i < childNum; i++) {
				if (Children [i].ItemTags != null && Children[i].ItemTags.Count > 0) {
					foreach (ItemTag it in Children[i].ItemTags) {
						tags [curIndex] = it;
						curIndex++;
					}
				}
				Children [i].GetAllItemTags (tags, curIndex);
				curIndex += Children [i].CountAll;
			}
		}
		///根据名字递归查找ItemTag，默认返回同名的第一个
		public ItemTag FindItemTag(string itemName){
			ICH result = FindICH (itemName);
			if (result == null || result.ItemTags == null || result.ItemTags.Count == 0) {
				Debug.Log ("Don't have ItemTag named " + itemName + " in Scene!");
				return null;
			}
			return result.ItemTags [0];
		}
		///根据名字递归查找List<ItemTag>
		public List<ItemTag> FindItemTags(string itemName){
			ICH result = FindICH (itemName);
			if (result == null || result.ItemTags == null || result.ItemTags.Count == 0) {
				Debug.Log ("Don't have ItemTag named " + itemName + " in Scene!");
				return null;
			}
			return result.ItemTags;
		}
		///根据名字递归查找ICH
		public ICH FindICH(string itemName, bool Countjj=false){
			if (HierarchyIndex != -1) {
				Debug.LogError ("You can only find a ItemTag from root!");
				return null;
			}
			string[] names = itemName.Split (GlobalValue.NameDivSymbol);
			ICH result = Countjj ? FindChildAndCount(names, 0) : FindChild (names, 0);
			return result;
		}
		//递归查找ICH
		ICH FindChild(string[] names, int childHie){
			if (childHie < names.Length) {
				int childIndex = GetChildIndex (names [childHie]);
				if (childIndex == -1) {
					return null;
				}
				return Children [childIndex].FindChild (names, childHie + 1);
			} else {
				return this;
			}
		}
		//递归查找ICH并沿路CountAll--
		ICH FindChildAndCount(string[] names, int childHie){
			if (childHie < names.Length) {
				countAll--;
				int childIndex = GetChildIndex (names [childHie]);
				if (childIndex == -1) {
					return null;
				}
				return Children [childIndex].FindChildAndCount (names, childHie + 1);
			} else {
				return this;
			}
		}
		///删除ItemTag
		public void RemoveItemTag(ItemTag it){
			//查找ItemTag并沿路countAll--
			ICH removeNode = FindICH (it.ItemName, true);
			if (removeNode == null) {
				return;
			} else {
				int len = removeNode.ItemTags.Count;
				for (int i = 0; i < len; i++) {
					if (removeNode.ItemTags [i] == it) {
						removeNode.ItemTags.RemoveAt (i);
						RemoveWhileEmpty ();
						return;
					}
				}
			}
		}
	}

	/// <summary>
	/// 一些全局值
	/// </summary>
	public class GlobalValue{
		///物品名的层次分隔符，如物品名为TaskItem_Ball_Football
		public const char NameDivSymbol = '_';
	}
}
